package ext.tianma.util;

import ext.tianma.bom.eco.BOMHelp;
import ext.tianma.part.forms.TMTypeUtil;
import wt.fc.WTObject;
import wt.part.WTPart;
import wt.util.WTException;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description PhantomPolarizerUtil: 申请虚拟偏光片料号时，产地增加校验功能，如果料号生效产地和上、
 *              下偏光片不一致时，系统提醒申请人告知虚拟偏光片组件必须和上、 下偏光片生效产地必须，并无法提交流程
 * @Author MaggieHao
 * @Date 2019/7/22 9:43
 * @Version 1.0
 **/
public class PhantomPolarizerUtil {

	public static final String PART_NUMBER_EFFECTIVE_SITE = "partNumberEffectiveSite";

	/***
	 * 任务提取校验 gx
	 * 
	 * @param obj
	 */
	public static void CheckPartSitForPart(WTObject obj) {
		try {
			if (obj instanceof WTPart) {
				WTPart part = (WTPart) obj;

				getPartSitForPart(part);

			}
		} catch (WTException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static String getPartSitForPart(WTPart part) throws WTException {

		System.out.println("校验偏光片生效地 beginning 料号：" + part.getNumber());
		// 获取虚拟料号生效地
		List<String> phantomPolarizeSiteList = IbaAttributeUtil.getAttributeValues(part, PART_NUMBER_EFFECTIVE_SITE);

		if (phantomPolarizeSiteList == null || phantomPolarizeSiteList.size() == 0) {
			throw new WTException("所获取的虚拟偏光片生效地为空");
		}
		System.out.println("虚拟偏光片料号生效地：" + phantomPolarizeSiteList);

		// 获取上下偏光片的料号
		String topPolarizeNumber = IbaAttributeUtil.getAttributeValue(part, "MtlNumofPOLTop");
		String botPolarizeNumber = IbaAttributeUtil.getAttributeValue(part, "MtlNumofPOLBtn");

		ext.tianma.bom.util.IBAUtil ibas = new ext.tianma.bom.util.IBAUtil(part);
		Map ibaMap = ibas.getIBAValues();
		if (!ibaMap.containsKey("MtlNumofPOLTop") || !ibaMap.containsKey("MtlNumofPOLBtn")) {
			System.out.println("11111111========上下偏光片属性为空，初始化。");
			return "上下偏光片属性为空";
		}

		if (topPolarizeNumber == null || topPolarizeNumber.length() == 0) {
			throw new WTException("上偏光片料号为空");
		}

		if (botPolarizeNumber == null || botPolarizeNumber.length() == 0) {
			throw new WTException("下偏光片料号为空");
		}

		WTPart topPolarizePart = null;
		WTPart botPolarizePart = null;
		List<String> topPolarizePartSiteList = null;
		List<String> botPolarizePartSiteList = null;
		try {
			topPolarizePart = BOMHelp.getPart(topPolarizeNumber, "", "Design");
			// 判断是否是偏光片
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (topPolarizePart == null || !TMTypeUtil.isPolarizer(topPolarizePart)) {
			throw new WTException("上偏光片部件不存在,或者不是偏光片！");
		}
		try {
			botPolarizePart = BOMHelp.getPart(botPolarizeNumber, "", "Design");
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (botPolarizePart == null || !TMTypeUtil.isPolarizer(botPolarizePart)) {
			throw new WTException("下偏光片部件不存在,或者不是偏光片！");
		}

		topPolarizePartSiteList = IbaAttributeUtil.getAttributeValues(topPolarizePart, PART_NUMBER_EFFECTIVE_SITE);
		if (topPolarizePartSiteList == null || topPolarizePartSiteList.size() == 0) {
			throw new WTException("上偏光片的生效地为空");
		}
		botPolarizePartSiteList = IbaAttributeUtil.getAttributeValues(botPolarizePart, PART_NUMBER_EFFECTIVE_SITE);
		if (botPolarizePartSiteList == null || botPolarizePartSiteList.size() == 0) {
			throw new WTException("下偏光片的生效地为空");
		}
		// 判断上下偏光片生效地是否相同
		compare(phantomPolarizeSiteList, topPolarizePartSiteList, botPolarizePartSiteList);
		System.out.println("校验偏光片生效地 ending");
		return "校验偏光片生效地 ending";
	}

	private static void compare(List<String> phantomPolarizeSites, List<String> topPolarizePartSites,
			List<String> botPolarizePartSites) throws WTException {
		Map<String, Boolean> map1 = new HashMap<String, Boolean>();
		Map<String, Boolean> map2 = new HashMap<String, Boolean>();
		String errMsg = "上偏光片料号生效地：" + topPolarizePartSites;
		String errMsg1 = ",下偏光片料号生效地：" + botPolarizePartSites;
		String errMsg2 = "虚拟偏光片料号生效地：" + phantomPolarizeSites;

		for (String str : topPolarizePartSites) {
			if (!map1.containsKey(str)) {
				map1.put(str, Boolean.FALSE);
			}
		}
		for (String str : botPolarizePartSites) {
			if (map1.containsKey(str)) {
				map1.put(str, Boolean.TRUE);
			}
		}
		for (String key : map1.keySet()) {
			System.out.println(key + ":" + map1.get(key));
			if (map1.get(key).equals(Boolean.TRUE)) {
				System.out.println("料号生效地相同为：" + key + ":" + map1.get(key));
				map2.put(key, Boolean.FALSE);
			}
		}
		// 如果map2为空，则上下偏光片无交集
		if (map2.isEmpty()) {
			throw new WTException("上下偏光片的料号生效地不同,不能创建此虚拟偏光片！\n" + errMsg + errMsg1);
		}

		for (String str : phantomPolarizeSites) {
			if (map2.containsKey(str)) {
				map2.put(str, Boolean.TRUE);
			}
		}
		Boolean b = false;
		for (String key : map2.keySet()) {
			System.out.println(key + ":" + map1.get(key));
			if (map2.get(key).equals(Boolean.TRUE)) {
				b = true;
				System.out.println("有相同的料号生效地:" + key + ":" + map2.get(key));
				break;
			}

		}
		if (b.equals(false)) {
			throw new WTException("虚拟偏光片的料号生效地和上下偏光片的料号生效地不同,不能创建此虚拟偏光片！\n" + errMsg + errMsg1 + "\n" + errMsg2);
		}
	}
}
